package com.cloudsigns.factorymanager2.produce.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cloudsigns.factorymanager2.BaseActivity;
import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.cusenum.CommonEnum;
import com.cloudsigns.factorymanager2.engineering.IView.IEngineeringUsersListView;
import com.cloudsigns.factorymanager2.engineering.adapter.PopWindowAdapter;
import com.cloudsigns.factorymanager2.engineering.bean.UserInfoBean;
import com.cloudsigns.factorymanager2.engineering.customview.SpinerPopWindow;
import com.cloudsigns.factorymanager2.engineering.presenter.EngineeringUserListPresenter;
import com.cloudsigns.factorymanager2.produce.bean.WorkPartBean;
import com.cloudsigns.factorymanager2.produce.bean.WorkPartOrderBean;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

public class ProduceEditDispatchInformationActivity extends BaseActivity implements AdapterView.OnItemClickListener, IEngineeringUsersListView {
    private RelativeLayout rl_back;
    private TextView tv_title;
    private RelativeLayout rl_dispatch_company;
    private EditText edt_dispatch_company, edt_dispatch_num, edt_receive_people;
    private ImageView img_receive_people;
    private RadioGroup rg_dispatch_way;
    private RadioButton rb_inner_dispatch, rb_external_dispatch;
    private Button btn_save;

    private SpinerPopWindow<String> mSpinerPopWindow;
    private PopWindowAdapter popWindowAdapter;
    private List<String> popStrLs = new ArrayList<String>();
    private UserInfoBean userInfoBean;
    private List<UserInfoBean> userInfoBeanList = new ArrayList<UserInfoBean>();
    private CommonEnum.ROLECATEGORY rolecategory;
    private String dispatch_way = "内派";
    private EngineeringUserListPresenter engineeringUserListPresenter;
    private WorkPartBean workPartBean;
    private WorkPartOrderBean workPartOrderBean = new WorkPartOrderBean();

    @Override
    protected void onBaseCreate(Bundle savedInstanceState) {
        setContentView(R.layout.layout_produce_edit_dispatch_information);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        rl_back = findViewById(R.id.rl_back);
        tv_title = findViewById(R.id.tv_title);
        rl_dispatch_company = findViewById(R.id.rl_dispatch_company);
        edt_dispatch_company = findViewById(R.id.edt_dispatch_company);
        edt_dispatch_num = findViewById(R.id.edt_dispatch_num);
        edt_receive_people = findViewById(R.id.edt_receive_people);
        img_receive_people = findViewById(R.id.img_receive_people);
        rg_dispatch_way = findViewById(R.id.rg_dispatch_way);
        rb_inner_dispatch = findViewById(R.id.rb_inner_dispatch);
        rb_external_dispatch = findViewById(R.id.rb_external_dispatch);
        btn_save = findViewById(R.id.btn_save);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        tv_title.setText(getResources().getString(R.string.edit_dispatch_information));

        engineeringUserListPresenter = new EngineeringUserListPresenter(this);

        if (null != getIntent()) {
            rolecategory = (CommonEnum.ROLECATEGORY) getIntent().getSerializableExtra(ConstantUtil.KEY_1);
            engineeringUserListPresenter.getUserList(rolecategory);

            workPartBean = (WorkPartBean) getIntent().getSerializableExtra(ConstantUtil.KEY_2);
        }

        mSpinerPopWindow = new SpinerPopWindow<String>(this, this);
        popWindowAdapter = new PopWindowAdapter(this, TAG);
        popWindowAdapter.setData(popStrLs);
        mSpinerPopWindow.setAdapter(popWindowAdapter);


        rg_dispatch_way.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, @IdRes int id) {
                if (id == R.id.rb_inner_dispatch) {
                    dispatch_way = "内派";
                    showDispatchContent();
                } else if (id == R.id.rb_external_dispatch) {
                    dispatch_way = "外派";
                    showDispatchContent();
                }
            }
        });

        rb_inner_dispatch.setChecked(true);

        int remainAmout = workPartBean.getAmount() - workPartBean.getWorkpartOrderProduceCount();

        edt_dispatch_num.setHint("<=" + remainAmout);

    }


    @Override
    public void setListener() {
        rl_back.setOnClickListener(this);
        img_receive_people.setOnClickListener(this);
        btn_save.setOnClickListener(this);
    }

    private void showDispatchContent() {
        if (dispatch_way.equals("内派")) {
            rl_dispatch_company.setVisibility(View.GONE);
        } else if (dispatch_way.equals("外派")) {
            rl_dispatch_company.setVisibility(View.VISIBLE);
        }
    }

    private void save() {
        int remainAmout = workPartBean.getAmount() - workPartBean.getWorkpartOrderProduceCount();
        if (TextUtils.isEmpty(edt_dispatch_num.getText().toString())) {
            showToast(getResources().getString(R.string.edit_dispatch_num));
        } else if (remainAmout < Integer.parseInt(edt_dispatch_num.getText().toString().trim())) {
            showToast(getResources().getString(R.string.out_of_num));
        } else if (Integer.parseInt(edt_dispatch_num.getText().toString().trim()) < 0 || Integer.parseInt(edt_dispatch_num.getText().toString().trim()) == 0) {
            showToast(getResources().getString(R.string.greater_than_zero));
        } else if (TextUtils.isEmpty(edt_receive_people.getText().toString())) {
            showToast(getResources().getString(R.string.choose_receive_people));
        } else {
            if (TextUtils.isEmpty(workPartOrderBean.getWorkPartOrderId())) {
                workPartOrderBean.setWorkPartOrderId(UUID.randomUUID().toString());
            }
            workPartOrderBean.setAmount(Integer.parseInt(edt_dispatch_num.getText().toString()));
            workPartOrderBean.setAssignReceiveAccountId(userInfoBean.getId());
            workPartOrderBean.setAssignReceiveAccountName(userInfoBean.getName());

            workPartBean.getWorkPartOrderList().add(workPartOrderBean);
            Intent mIntent = new Intent();
            mIntent.putExtra("dispatch_way", dispatch_way);
            if (dispatch_way.equals("内派")) {
                mIntent.putExtra(ConstantUtil.KEY_1, workPartBean);
                mIntent.putExtra(ConstantUtil.KEY_2, workPartOrderBean);
            } else if (dispatch_way.equals("外派")) {

            }
            setResult(ConstantUtil.START_ACTIVITY_KEY_1, mIntent);
            finish();
        }
    }

    @Override
    public void widgetClick(View view) {
        switch (view.getId()) {
            case R.id.rl_back:
                finish();
                break;
            case R.id.img_receive_people:
                hideSoftInputView();
                mSpinerPopWindow.setWidth(edt_receive_people.getWidth());
                mSpinerPopWindow.showAsDropDown(edt_receive_people);
                break;
            // 保存
            case R.id.btn_save:
                save();
                break;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        edt_receive_people.setText(popStrLs.get(i));
        userInfoBean = userInfoBeanList.get(i);
        mSpinerPopWindow.dismiss();
    }

    @Override
    public void getDataSuccessed(List<UserInfoBean> dataLs) {
        if (null != dataLs) {
            userInfoBeanList.clear();
            userInfoBeanList.addAll(dataLs);
            for (UserInfoBean userInfoBean : userInfoBeanList) {
                popStrLs.add(userInfoBean.getName() + ":" + userInfoBean.getId());
            }

            popWindowAdapter.setData(popStrLs);
            popWindowAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void showFailedError(final String desc) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                showToast(desc);
            }
        });
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }
}
